package com.example.onlinemusicserver2024.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.onlinemusicserver2024.common.R;
import com.example.onlinemusicserver2024.mapper.CollectMapper;
import com.example.onlinemusicserver2024.model.domain.Collect;
import com.example.onlinemusicserver2024.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect> implements CollectService {
    @Autowired
    CollectMapper collectMapper;

    @Override
    public R collectionOfUser(int userId) {
        // 根据userId拼接SQL查询条件
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        // 执行查询,得到结果集
        List<Collect> lstCollect = collectMapper.selectList(queryWrapper);
        return R.success("用户收藏记录", lstCollect);

    }

    @Override

    public R addCollection(Collect collect) {
        collect.setCreateTime(new Date());
        if (collectMapper.insert(collect) > 0) {
            return R.success("收藏成功");
        } else {
            return R.error("收藏失败");
        }
    }

    @Override
    public R getCollectionStatus(Collect collect) {
        int userId = collect.getUserId();
        int songId = collect.getSongId();
        int type = collect.getType();
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("song_id", songId);
        queryWrapper.eq("type", type);
        List<Collect> collects = collectMapper.selectList(queryWrapper);
        if (collects.size() > 0) {
            return R.success("查询成功", collects);
        } else {
            return R.error(null);
        }

    }

    @Override
    public R deleteCollection(int userId, int songId) {
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("song_id",songId);
        if(collectMapper.delete(queryWrapper) > 0){
            return R.success("取消成功");
        }else{
            return R.error("取消失败");
        }
    }
}
